package com.yanqu.road.server.manger.mail;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.mail.AllMailLanguage;
import com.yanqu.road.logic.bussiness.mail.ServerMailBussiness;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.ServerMail;
import com.yanqu.road.entity.player.UserMail;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.mail.MailModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.utils.UUIDHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ServerMailMgr extends TempMgr {

	public static Map<Integer, ServerMail> serverMailMap = new ConcurrentHashMap<>();
	private static Map<Integer, Map<String, AllMailLanguage>> allMailLanguageMap;

	@Override
	public boolean reloadData() {
		serverMailMap = ServerMailBussiness.getServerMailMap();
		allMailLanguageMap = ServerMailBussiness.getAllMailsLanguage();
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}

	public boolean save() {
		return true;
	}

	public static void playerGetServerMail(GamePlayer player) {
		AttributeModule attModule = player.getModule(AttributeModule.class);
		long maxServerMailId = attModule.getAttribute(ePlayerAttrType.MaxServerMailId);
		long newMaxId = 0;
		for (ServerMail serverMail : serverMailMap.values()) {
			if (serverMail.getMailId() <= maxServerMailId) {
				continue;
			}
			if (serverMail.getSendTime().getTime() / 1000 < player.getModule(PlayerModule.class).getUserInfo().getRegisterTime()) {
				continue;
			}
			if (MailManager.isMailExpired(serverMail)) {
				continue;
			}

			if (!serverMail.getAppIdSet().isEmpty() && !serverMail.getAppIdSet().contains(player.getUserInfo().getAppId())) {
				continue;
			}

			UserMail userMail = getUserMail(player, serverMail);

			player.getModule(MailModule.class).addUserMail(userMail);
			if (newMaxId < serverMail.getMailId()) {
				newMaxId = serverMail.getMailId();
			}
		}
		if (newMaxId != 0) {
			attModule.setAttribute(ePlayerAttrType.MaxServerMailId, newMaxId);
		}
	}

	private static UserMail getUserMail(GamePlayer player, ServerMail serverMail) {
		AllMailLanguage allMailLanguage = getMailLanguage(serverMail.getMailId(), player.getLanguage());
		UserMail userMail = new UserMail();
		userMail.setMailUid(UUIDHelper.randomUUID(String.valueOf(GameServer.getInstance().getServerId())));
		userMail.setMailType(serverMail.getMailType());
		userMail.setTitle(allMailLanguage == null ? serverMail.getTitle() : allMailLanguage.getTitle());
		userMail.setContent(allMailLanguage == null ? serverMail.getContent() : allMailLanguage.getMsg());
		userMail.setAnnexes(serverMail.getAnnexes());
		userMail.setSendTime(serverMail.getSendTime());
		userMail.setSendUserId(0);
		userMail.setSendNickName(MailManager.SYSTEM_ADMIN);
		userMail.setReceiveUserId(player.getUserId());
		userMail.setRead(false);
		userMail.setReadTime(new Date());
		userMail.setGetReward(false);
		userMail.setDelete(false);
		userMail.setPersonalId(serverMail.getMassId());
		userMail.setInsertOption();
		return userMail;
	}


	public static AllMailLanguage getMailLanguage(int mailId, String language) {
		if(null == allMailLanguageMap){
			return null;
		}
		Map<String, AllMailLanguage> dataMap = allMailLanguageMap.get(mailId);
		if(null == dataMap){
			return null;
		}
		return dataMap.get(language);
	}

	public static void addServerMail(ServerMail serverMail, JSONObject languageObj) {
		ServerMailBussiness.addServerMail(serverMail);
		serverMailMap.put(serverMail.getMailId(), serverMail);
		// 生成语言模板
		Map<String, AllMailLanguage> map = new ConcurrentHashMap<>();
		for (Object k : languageObj.keySet()) {
			String language = (String)k;
			JSONObject jsonObject = languageObj.getJSONObject(language); // 语言对象 对象内有title msg
			AllMailLanguage allMailLanguage = initAllMailLanguage(serverMail, language, jsonObject);
			ServerMailBussiness.addAllServerMailLanguage(allMailLanguage);
			map.put(language, allMailLanguage);
		}
		allMailLanguageMap.put(serverMail.getMailId(), map);
	}

	private static AllMailLanguage initAllMailLanguage(ServerMail serverMail, String language, JSONObject jsonObject) {
		AllMailLanguage data = new AllMailLanguage();
		data.setMailId(serverMail.getMailId());
		data.setLanguageType(language);
		data.setTitle(jsonObject.getString("title"));
		data.setMsg(jsonObject.getString("msg"));
		return data;
	}

	//构建全服邮件
	public static ServerMail buildServerMail(int type, String title, String content, String annexes) {
		ServerMail serverMail = new ServerMail();
		serverMail.setMailType(type);
		serverMail.setTitle(title);
		serverMail.setContent(content);
		serverMail.setAnnexes(annexes);
		serverMail.setSendTime(new Date());
		serverMail.setDelete(false);
		serverMail.setInsertOption();
		return serverMail;
	}

	public static Map<Integer, ServerMail> getServerMailMap() {
		return serverMailMap;
	}

	public static Map<Integer, Map<String, AllMailLanguage>> getAllMailLanguageMap() {
		return allMailLanguageMap;
	}
}
